var map = null; 
	var dirPanel;
	
	var vertexIconStyle;
	var depIconStyle;
	
	function setIcons() {
	  // icona luogo attarversato
	  var vertexIcon = new GIcon();
	  vertexIcon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
	  vertexIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	  vertexIcon.iconSize = new GSize(12, 20);
	  vertexIcon.shadowSize = new GSize(22, 20);
	  vertexIcon.iconAnchor = new GPoint(6, 20);
	  vertexIcon.infoWindowAnchor = new GPoint(5, 1);
	  vertexIconStyle = { icon:vertexIcon };
	 
	  	 
	  // icona deposito
	  var depIcon = new GIcon();
	  depIcon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
	  depIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	  depIcon.iconSize = new GSize(12, 20);
	  depIcon.shadowSize = new GSize(22, 20);
	  depIcon.iconAnchor = new GPoint(6, 20);
	  depIcon.infoWindowAnchor = new GPoint(5, 1);
	  depIconStyle = { icon:depIcon };
	}
	
		
	function setupMap(tratte) {
//	if (map != null) {
	      var j;
	      var l; 
	      var route=[];
	      var idMissione; 
	      var depot = new Array();
    	
          if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map_canvas"));
		    directionsPanel = document.getElementById("directions");
            map.addControl(new GLargeMapControl());
            map.setCenter(new GLatLng(41.9, 12.483333), 5);
            map.enableDoubleClickZoom();
    		
		    geocoder = new GClientGeocoder();

		    setIcons();
		    var rouEl = 0;
		    //da modificare
		    var schedule = new Array();
		    //alert(tratte[0].length-1)
		    for(j=0;j<tratte[0].length; j++)
			    {	
				    eval("schedule["+j+"] = ["+parseFloat(tratte[rouEl][j].replace(',','.'))+","+parseFloat(tratte[rouEl+1][j].replace(',','.'))+"];")
			    }
		    depot[0]=parseFloat(schedule[0][0]);
		    depot[1]=parseFloat(schedule[0][1]);
    		
            sched = new GDirections(null,dirPanel);
            GEvent.addListener(sched,"load", function() { 
              var poly=sched.getPolyline(); 
		      poly.color = "#000000"; 
              map.addOverlay(poly); 
            });
    	
    				
		    sched.loadFromWaypoints(schedule,{getPolyline:true});
		    //createDepotMarker(schedule[0]);
		    addDepMarker(depot[1], depot[0], idMissione, deposito);
		    for (i = 1; i < tratte[0].length; i++)
		      {
		       addVertexMarker(schedule[i][1],schedule[i][0]);
		      }	
          
	      }
//	  }
    }
		
	//  aggiungi marker per i luoghi attraversati
	function addVertexMarker(lat,lon) {
		var marker = new GMarker(new GPoint(lat,lon),vertexIconStyle);
		map.addOverlay(marker);
    }
	
	//  aggiungi marker per deposito
	function addDepMarker(lat, lon, l, deposito) {

		var marker = new GMarker(new GPoint(lat, lon),depIconStyle);
		var myHtml = "<b> Missione n.ro " + deposito[1][0] + "</b><br/>" 
		               + " Veicolo: " + deposito[2][0] + " " 
					   + " (" + deposito[3][0] + ")<br/>" 
					   + " Deposito: " + deposito[0][0] + "; <br/>"
					   
       
		map.addOverlay(marker);
		marker.openInfoWindowHtml(myHtml);

    }

	function addVertexToMap(response) {

        var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        
		var marker = new GMarker(point,vertexIconStyle);
		map.addOverlay(marker);
    }
	
	
	
	function addDepToMap(response) {

        var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        
		var marker = new GMarker(point,depIconStyle);
		var myHtml = "<b> Missione n.ro " + "@@@" + "</b><br/>" 
		               + " Veicolo " + "@@@" + "<br/>" 
					   + " Deposito " + "@@@" + "<br/>"  ;
        
		map.addOverlay(marker);
		marker.openInfoWindowHtml(myHtml);
    }
	
	function createVertexMarker(addrV) {  
      var geocoder1 = new GClientGeocoder();
	  geocoder1.getLocations(addrV, addVertexToMap);
	}
	
